<template>
	<view class="container">
		<view class="example">
			<uni-forms ref="form" :model="form" labelWidth="80px">
				<uni-forms-item label="报名班级" name="classId">
					<uni-data-select v-model="form.classId" :localdata="range"></uni-data-select>
				</uni-forms-item>
				<uni-forms-item label="姓名" name="name">
					<uni-easyinput v-model="form.name" placeholder="请输入报名学员姓名" />
				</uni-forms-item>
				<uni-forms-item label="手机号码" name="phone">
					<uni-easyinput v-model="form.phone" placeholder="请输入手机号码" />
				</uni-forms-item>
				<uni-forms-item label="职务" name="job">
					<uni-easyinput v-model="form.job" placeholder="请输入职务" />
				</uni-forms-item>
			</uni-forms>
			<button type="primary" @click="submit" v-show="showSubmit">提交</button>
		</view>
	</view>
</template>

<script>
	import {
		getInfoId
	} from '@/utils/auth';
	import {
		getClassInfoAll
	} from '@/api/jz/class/class'
	import {
		addInfo as addStudentInfo
	} from '@/api/jz/apply/info'

	export default {
		data() {
			return {
				showSubmit: true,
				range: [],
				form: {
					schoolId: null,
				},
				classInfoList: [],
				rules: {
					phone: {
						rules: [{
							required: true,
							errorMessage: '手机号码不能为空'
						}, {
							pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
							errorMessage: '请输入正确的手机号码'
						}]
					},
					name: {
						rules: [{
							required: true,
							errorMessage: '姓名不能为空'
						}]
					},
					job: {
						rules: [{
							required: true,
							errorMessage: '职务不能为空'
						}]
					},
					// classId: {
					// 	rules: [{
					// 		required: true,
					// 		errorMessage: '请选择班级'
					// 	}]
					// }
				}
			}
		},
		onLoad() {
			this.form.schoolId = getInfoId()
			this.getClassInfoAllFun();
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			getClassInfoAllFun() {
				getClassInfoAll().then(response => {
					if (response.code === 200) {
						this.classInfoList = response.rows;
						let classKeyValueList = [];
						for (let i = 0; i < this.classInfoList.length; i++) {
							let classOne = {};
							classOne.value = this.classInfoList[i].id
							classOne.text = this.classInfoList[i].className
							classKeyValueList.push(classOne);
						}
						this.range = classKeyValueList;
					}
				})
			},
			addStudentInfoFun(data) {
				addStudentInfo(data).then(response => {
					if (response.code === 200) {
						this.$modal.msgSuccess("报名成功,等待审核")
						uni.navigateBack({
							delta: 1
						})
					}

				})
			},
			submit() {
				this.$refs.form.validate().then(res => {
					this.showSubmit = false;
					this.form.status = -1;
					this.addStudentInfoFun(this.form);
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #ffffff;
	}

	.example {
		padding: 15px;
		background-color: #fff;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
		flex: 1;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		line-height: 35px;
		margin-left: 10px;
	}
</style>